package org.jeecg.modules.procurement.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.procurement.entity.BussMaterialApplicationDetail;
import org.jeecg.modules.procurement.entity.BussMaterialProcurementPlan;
import org.jeecg.modules.procurement.entity.BussMaterialProcurementPlanDetail;
import org.jeecg.modules.procurement.mapper.BussMaterialApplicationDetailMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementPlanDetailMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementPlanMapper;
import org.jeecg.modules.procurement.service.IBussMaterialProcurementPlanService;
import org.jeecg.modules.procurement.vo.BussMaterialProcurementPlanDetailPage;
import org.jeecg.modules.procurement.vo.BussMaterialProcurementPlanPage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 原料采购计划单
 * @Author: jeecg-boot
 * @Date:   2024-06-06
 * @Version: V1.0
 */
@Service
public class BussMaterialProcurementPlanServiceImpl extends ServiceImpl<BussMaterialProcurementPlanMapper, BussMaterialProcurementPlan> implements IBussMaterialProcurementPlanService {

	@Autowired
	private BussMaterialProcurementPlanMapper bussMaterialProcurementPlanMapper;
	@Autowired
	private BussMaterialProcurementPlanDetailMapper bussMaterialProcurementPlanDetailMapper;
	@Autowired
	private BussMaterialApplicationDetailMapper bussMaterialApplicationDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveMain(BussMaterialProcurementPlan bussMaterialProcurementPlan, List<BussMaterialProcurementPlanDetailPage> bussMaterialProcurementPlanDetailList) {
		//获取计划中的数量
		Map<String, BigDecimal> map = new HashMap<>();
		for (BussMaterialProcurementPlanDetailPage entity:bussMaterialProcurementPlanDetailList){
			String keyToCheck = entity.getApplicationDetailId();
			if (map.containsKey(keyToCheck)) {
				BigDecimal num = map.get(keyToCheck).add(entity.getNum());
				map.put(keyToCheck,num);
			} else {
				map.put(keyToCheck, entity.getNum());
			}
		}
		//获取请购单跟已经添加到计划中的请购单数量
		for (String key : map.keySet()) {
			BussMaterialApplicationDetail detail = bussMaterialApplicationDetailMapper.selectById(key);
			List<BussMaterialProcurementPlanDetailPage> addDetail = bussMaterialProcurementPlanDetailMapper.selectDetailNum(key);
			BigDecimal addNum = new BigDecimal("0");
			for (BussMaterialProcurementPlanDetailPage detailPage: addDetail){
				addNum = addNum.add(detailPage.getNum());
			}
			addNum = map.get(key).add(addNum);
			int res = addNum.compareTo(detail.getNum());
			if (res > 0){
				return 9;
			}
		}
		int res = bussMaterialProcurementPlanMapper.insert(bussMaterialProcurementPlan);
		if(bussMaterialProcurementPlanDetailList!=null && bussMaterialProcurementPlanDetailList.size()>0) {
			for(BussMaterialProcurementPlanDetailPage entity:bussMaterialProcurementPlanDetailList) {
				BussMaterialProcurementPlanDetail detail = new BussMaterialProcurementPlanDetail();
				BeanUtils.copyProperties(entity, detail);
				detail.setId(String.valueOf(IdWorker.getId()));
				//外键设置
				detail.setPlanId(bussMaterialProcurementPlan.getId());
				bussMaterialProcurementPlanDetailMapper.insert(detail);
			}
		}
		return res;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateMain(BussMaterialProcurementPlan bussMaterialProcurementPlan,List<BussMaterialProcurementPlanDetailPage> bussMaterialProcurementPlanDetailList) {
		//获取计划中的数量
		Map<String, BigDecimal> map = new HashMap<>();
		for (BussMaterialProcurementPlanDetailPage entity:bussMaterialProcurementPlanDetailList){
			String keyToCheck = entity.getApplicationDetailId();
			if (map.containsKey(keyToCheck)) {
				BigDecimal num = map.get(keyToCheck).add(entity.getNum());
				map.put(keyToCheck,num);
			} else {
				map.put(keyToCheck, entity.getNum());
			}
		}
		//获取请购单跟已经添加到计划中的请购单数量（不包含当前计划单）
		for (String key : map.keySet()) {
			BussMaterialApplicationDetail detail = bussMaterialApplicationDetailMapper.selectById(key);
			List<BussMaterialProcurementPlanDetailPage> addDetail = bussMaterialProcurementPlanDetailMapper.selectProcurementDetailNum(bussMaterialProcurementPlan.getId(),key);
			BigDecimal addNum = new BigDecimal("0");
			for (BussMaterialProcurementPlanDetailPage detailPage: addDetail){
				addNum = addNum.add(detailPage.getNum());
			}
			addNum = map.get(key).add(addNum);
			int res = addNum.compareTo(detail.getNum());
			if (res > 0){
				return 9;
			}
		}
		int res = bussMaterialProcurementPlanMapper.updateById(bussMaterialProcurementPlan);
		
		//1.先删除子表数据
		bussMaterialProcurementPlanDetailMapper.deleteByMainId(bussMaterialProcurementPlan.getId());
		
		//2.子表数据重新插入
		if(bussMaterialProcurementPlanDetailList!=null && bussMaterialProcurementPlanDetailList.size()>0) {
			for(BussMaterialProcurementPlanDetailPage entity:bussMaterialProcurementPlanDetailList) {
				BussMaterialProcurementPlanDetail detail = new BussMaterialProcurementPlanDetail();
				BeanUtils.copyProperties(entity, detail);
				//外键设置
				detail.setPlanId(bussMaterialProcurementPlan.getId());
				bussMaterialProcurementPlanDetailMapper.insert(detail);
			}
		}
		return res;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bussMaterialProcurementPlanDetailMapper.deleteByMainId(id);
		bussMaterialProcurementPlanMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bussMaterialProcurementPlanDetailMapper.deleteByMainId(id.toString());
			bussMaterialProcurementPlanMapper.deleteById(id);
		}
	}

	@Override
	public IPage<BussMaterialProcurementPlanPage> myPage(Page<BussMaterialProcurementPlan> page, QueryWrapper<BussMaterialProcurementPlan> queryWrapper) {
		return this.baseMapper.myPage(page, queryWrapper);
	}


}
